package com.guage.producter.controller;

import java.sql.Timestamp;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.guage.producter.domain.Students;
import com.guage.producter.domain.StudentsKey;
import com.guage.producter.result.JSONResult;
import com.guage.producter.service.StudentsService;
import com.guage.producter.utils.SnowflakeIdWorker;
import com.pad.dpscn.daag.sdk.core.annotation.DataSendPosition;

import lombok.extern.slf4j.Slf4j;

/**
 * @desc
 * @author yangdechao
 * @since 2024-2-27
 * 主要用于联合主键测试
 */
@RestController
@RequestMapping("/students")
@Slf4j
@DataSendPosition
public class StudentsController {

	@Autowired
	private StudentsService studentsService;

	/**
	 * 保存和修改操作公用此方法
	 * 
	 * @param operLog 前端传递来的实体数据
	 */
	@GetMapping(value = "/selectById/{id}")
	public JSONResult selectById(@PathVariable("id") String id) {
		StudentsKey key = new StudentsKey();
		Students entity = studentsService.selectByPrimaryKey(key);
		return JSONResult.success(entity);
	}

	/**
	 * 保存和修改操作公用此方法
	 * 
	 * @param operLog 前端传递来的实体数据
	 */
	@PostMapping(value = "/save")
	public JSONResult save(@RequestBody Students students) {
		log.info("======================业务执行开始=====================");
		
		SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 1);
		long id1 = idWorker.nextId();
		students.setStudentId(id1+"");
		long id2 = idWorker.nextId();
		students.setCourseId(id2+"");
		studentsService.insertSelective(students);
		log.info("======================业务执行结束=====================");

		return JSONResult.success();
	}
	
	/**
	 * 保存和修改操作公用此方法
	 * 
	 * @param operLog 前端传递来的实体数据
	 */
	@PostMapping(value = "/updateById")
	public JSONResult updateById(@RequestBody Students students) {
		log.info("======================业务执行开始=====================");
		// 获取当前的日期和时间（Date对象）  
        Date now = new Date();  
        // 将Date对象转换为Timestamp对象  
        Timestamp timestamp = new Timestamp(now.getTime()); 
		students.setUpdateTime(timestamp);
		studentsService.updatByPrimaryKeySelective(students);
		log.info("======================业务执行结束=====================");

		return JSONResult.success();
	}

}